package com.example.coreshopapp.base;

import android.app.AlertDialog;
import android.content.Context;
import android.view.View;

public class AlertDialogSingleton {
    // 静态实例
    private static AlertDialogSingleton instance;
    private AlertDialog dialog;

    // 私有构造函数
    private AlertDialogSingleton() {}

    // 获取单例实例
    public static synchronized AlertDialogSingleton getInstance() {
        if (instance == null) {
            synchronized (AlertDialogSingleton.class) {
                if (instance == null) {
                    instance = new AlertDialogSingleton();
                }
            }
        }
        return instance;
    }

    // 创建并显示 AlertDialog
    public AlertDialog createAlertBuilder(Context context, int layout) {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss(); // 关闭已显示的对话框
        }

        View view = View.inflate(context, layout, null);
        dialog = new AlertDialog.Builder(context)
                .setView(view)
                .create();
        dialog.show();
        return dialog;
    }

    // 关闭对话框
    public void dismissDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }
}
